var t = getApp();

Page({
    data: {
        indentBoo: !0,
        TypeName: "无",
        height: 34,
        userAddrDto: {},
        billContent: "",
        integral: 0,
        isIntegralToMoney: 0,
        reduceMoney: 0,
        switchFlag: !1
    },
    onShareAppMessage: function(e) {
        return t.globalData.share;
    },
    onLoad: function() {
        var e = this, a = wx.getStorageSync("logs") || "";
        a || (t.getToken(), a = wx.getStorageSync("logs")), this.setData({
            token: a
        });
        try {
            wx.removeStorageSync("addrList");
        } catch (t) {}
        var o = wx.getStorageSync("cartSessionId") || "", n = wx.getStorageSync("carData"), r = n.userAddrDto, i = n.postFee, s = n.cartInfos, d = n.integraldVo, c = s.length - 1;
        s.forEach(function(t, a) {
            e.getPromotion(t.id, s, a, e, c);
        }), this.setData({
            cartSessionId: o,
            carData: n,
            userAddrDto: r,
            postFee: i,
            integraldVo: d
        });
    },
    onShow: function() {
        var t = wx.getStorageSync("billType") || "", e = wx.getStorageSync("billTitle") || "", a = wx.getStorageSync("billContent3") || "", o = wx.getStorageSync("checked"), n = this.data.TypeName;
        o ? 1 == t ? n = "个人/普通" : 2 == t && (n = "单位") : n = "无", this.setData({
            billType: t,
            billTitle: e,
            billTitle3: a,
            indentBoo: !0,
            TypeName: n
        });
        var r = wx.getStorageSync("addrList") || "";
        if (r) {
            var i = "";
            r.forEach(function(t, e) {
                t.currentDefault && (i = e);
            });
            var s = r[i] || "";
            s != this.data.userAddrDto && this.setData({
                userAddrDto: s
            });
        }
    },
    onHide: function() {},
    onUnload: function() {
        try {
            wx.removeStorageSync("addrList");
        } catch (t) {}
        try {
            wx.removeStorageSync("carData");
        } catch (t) {}
        try {
            wx.removeStorageSync("billTitle");
        } catch (t) {}
        try {
            wx.removeStorageSync("billContent");
        } catch (t) {}
        try {
            wx.removeStorageSync("billContent3");
        } catch (t) {}
        try {
            wx.removeStorageSync("billType");
        } catch (t) {}
        try {
            wx.removeStorageSync("checked");
        } catch (t) {}
    },
    changeAddr: function() {
        this.setData({
            boo: !1
        });
        wx.navigateTo({
          url: "/hyb_yl/shopsub/pages/address/address",
            success: function() {}
        });
    },
    visitorsBook: function(t) {
        this.setData({
            visitors: t.detail.value
        });
    },
    clickUrl: function(e) {
        wx.showLoading({
            title: "提交中...",
            mask: !0
        });
        var a = this, o = a.data.carData.orderId, n = a.data.carData.postFee, r = a.data.carData.orderType, i = a.data.userAddrDto.addrId, s = [], d = a.data.cartInfos, c = a.data.userAddrDto.area ? a.data.userAddrDto.area + a.data.userAddrDto.receiverAddress : a.data.userAddrDto.receiverAddress;
        d.length >= 0 && d.forEach(function(t) {
            s.push(t.name + "|" + t.price + "|" + t.num + "|" + t.price * t.num);
        }), "" != a.data.billType && a.setData({
            billContent: s.join(",")
        }), i ? (a.data.indentBoo && wx.request({
            url: t.globalData.httpOne + "mall/order/submit",
            method: "POST",
            header: {
                "Content-Type": "application/json"
            },
            data: {
                token: a.data.token,
                billType: a.data.billType,
                billTitle: a.data.billTitle,
                billContent: a.data.billContent,
                identfyNumber: a.data.billTitle3,
                paymentType: 3,
                orderId: o,
                cartSessionId: a.data.cartSessionId,
                buyerMessage: a.data.visitors || "",
                postFee: n,
                orderType: r,
                addrId: i,
                address: c,
                isIntegralToMoney: a.data.isIntegralToMoney
            },
            success: function(t) {
                if (console.log(t), 200 == t.data.code && t.data.data) {
                    var e = t.data.data.wxPayOrderResult;
                    try {
                        wx.setStorageSync("indentList", t.data.data);
                    } catch (t) {}
                    if (!e) return wx.showToast({
                        title: "支付成功",
                        icon: "none",
                        duration: 1500
                    }), void setTimeout(function() {
                        wx.redirectTo({
                            url: "/pages/shopsub/pages/oss/oss?orderId=" + o
                        });
                    }, 1e3);
                    wx.requestPayment({
                        appId: e.appId,
                        timeStamp: e.timeStamp,
                        nonceStr: e.nonceStr,
                        package: e.packageValue,
                        signType: e.signType,
                        paySign: e.paySign,
                        success: function(t) {
                            "requestPayment:ok" == t.errMsg && (wx.showToast({
                                title: "支付成功",
                                icon: "none",
                                duration: 1500
                            }), setTimeout(function() {
                                wx.redirectTo({
                                    url: "/pages/shopsub/pages/oss/oss?orderId=" + o
                                });
                            }, 1e3));
                        },
                        fail: function(t) {
                            if ("requestPayment:fail cancel" == t.errMsg) {
                                try {
                                    wx.removeStorageSync("indentList");
                                } catch (t) {}
                                wx.redirectTo({
                                    url: "/pages/shopsub/pages/order/order"
                                });
                            }
                        }
                    });
                } else wx.showToast({
                    title: "系统繁忙，请稍后重试",
                    icon: "none",
                    duration: 1e3
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: "系统繁忙，请稍重试",
                    icon: "none",
                    duration: 1e3
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        }), a.setData({
            indentBoo: !1
        })) : wx.showToast({
            title: "请选择收货地址",
            icon: "none",
            duration: 2e3
        });
    },
    addCount: function(e) {
        wx.showLoading({
            mask: !0
        }), this.setData({
            switchFlag: !1,
            reduceMoney: 0,
            isIntegralToMoney: 0
        });
        var a = this, o = e.currentTarget.dataset.index, n = e.target.id, r = this.data.cartInfos, i = r[o].num;
        wx.request({
            url: t.globalData.httpOne + "mall/cart/decreOrIncre",
            data: {
                itemId: n,
                itemCount: 1,
                index: o,
                type: 1,
                cartSessionId: a.data.cartSessionId,
                orderId: a.data.carData.orderId,
                orderType: a.data.carData.orderType
            },
            success: function(t) {
                200 == t.data.code && (wx.hideLoading(), i += 1, r[o].num = i, a.setData({
                    cartInfos: r
                }), a.getTotalPrice());
            },
            fail: function() {
                wx.showToast({
                    title: "系统繁忙，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    minusCount: function(e) {
        wx.showLoading({
            mask: !0
        });
        var a = this, o = e.currentTarget.dataset.index, n = e.target.id, r = this.data.cartInfos, i = r[o].num;
        if (i <= 1) return wx.showToast({
            title: "至少一件商品",
            icon: "none",
            duration: 1e3
        }), !1;
        this.setData({
            switchFlag: !1,
            reduceMoney: 0,
            isIntegralToMoney: 0
        }), wx.request({
            url: t.globalData.httpOne + "mall/cart/decreOrIncre",
            data: {
                itemId: n,
                itemCount: 1,
                index: o,
                type: 2,
                cartSessionId: a.data.cartSessionId,
                orderId: a.data.carData.orderId,
                orderType: a.data.carData.orderType
            },
            success: function(t) {
                200 == t.data.code && (wx.hideLoading(), i -= 1, r[o].num = i, a.setData({
                    cartInfos: r
                }), a.getTotalPrice());
            },
            fail: function() {
                wx.showToast({
                    title: "系统繁忙，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    getTotalPrice: function() {
        for (var t = this.data.cartInfos, e = 0, a = 0, o = 0, n = 0; n < t.length; n++) {
            if (e = t[n].num * t[n].price, o += t[n].num * t[n].price, t[n].moneyOffList) for (var r = 0; r < t[n].moneyOffList.length; r++) t[n].num * t[n].price / 100 >= t[n].moneyOffList[r].promCondition / 100 ? (e = t[n].num * t[n].price - t[n].moneyOffList[r].promValue, 
            t[n].promCondition = t[n].moneyOffList[r].promCondition / 100, t[n].promValue = t[n].moneyOffList[r].promValue / 100) : t[n].num * t[n].price / 100 < t[n].promCondition && t[n].promCondition == t[n].moneyOffList[0].promCondition / 100 && (t[n].promCondition = t[n].moneyOffList[0].promCondition / 100, 
            t[n].promValue = t[n].moneyOffList[0].promValue / 100);
            a += e;
        }
        this.setData({
            cartInfos: t,
            totalPrice: a,
            inTotal: o
        });
    },
    bindlinechange: function(t) {
        var e = 2 * t.detail.height;
        this.setData({
            height: e
        });
    },
    getPromotion: function(e, a, o, n, r) {
        wx.request({
            url: t.globalData.httpOne + "mall/prom/itemPromInfo",
            data: {
                itemId: e
            },
            success: function(t) {
                if (200 == t.data.code) {
                    var e = t.data.data, r = [], i = {};
                    if (e && e.forEach(function(t) {
                        "240000" == t.cdActiveType && r.push(t);
                    }), r.length > 0) {
                        for (var s = 0; s < r.length; s++) s + 1 < r.length && r[s].priorityLevel < r[s + 1].priorityLevel && (i = r[s], 
                        r[s] = r[s + 1], r[s + 1] = i);
                        a[o].moneyOffList = r, a[o].promCondition = r[0].promCondition / 100, a[o].promValue = r[0].promValue / 100;
                    }
                }
                n.setData({
                    cartInfos: a
                }), n.getTotalPrice();
            }
        });
    },
    switchChange: function(e) {
        var a = this;
        if (console.log(e), e.detail.value) {
            var o = 0;
            this.data.cartInfos.forEach(function(t) {
                o += t.useIntegral * t.num;
            }), wx.request({
                url: t.globalData.httpOne + "mall/order/get/integralToMoney",
                data: {
                    token: a.data.token,
                    totalMoney: a.data.totalPrice,
                    totalIntegral: o
                },
                success: function(t) {
                    console.log(t), 200 == t.data.code && a.setData({
                        integral: t.data.data.integral,
                        reduceMoney: t.data.data.money,
                        isIntegralToMoney: 1,
                        switchFlag: !0
                    });
                }
            });
        } else this.setData({
            reduceMoney: 0,
            isIntegralToMoney: 0
        });
    }
});